local Account = class("Account", function()
    return require("ds/DataBase"):new()
end)
 
Account.evtConnectResponse = "AccountConnectResponse"
Account.evtMainHallLogin = "MAIN_HALL_MSG_LOGIN"

Account.m_UserLoginInfo = nil
Account.m_GameList = {} 

function Account:init()
  self:onAddListener()
  
  return true
end

function Account:onAddListener()
    tools.Event:attach(ds.Account.evtConnectResponse, handler(self, self.onConnectResponse), "Ds_Account_Response")
    tools.Event:attach(ds.Account.evtMainHallLogin, handler(self, self.onMainHallLogin), "Ds_Account_MainHallLogin")
   
end

function Account:onRemoveListener()
    tools.Event:removeByEventNameAndObserverName(ds.Account.evtConnectResponse, "Ds_Account_Response")
    tools.Event:removeByEventNameAndObserverName(ds.Account.evtMainHallLogin, "Ds_Account_MainHallLogin")
    
end

function Account:destroy()
  self:onRemoveListener()
end

function Account:getUserLoginInfo()
    return self.m_UserLoginInfo
end

function Account:getGameList()
    return self.m_GameList
end

function Account:sendConnect(sIp , wPort )
    print("===sendConnect==="..sIp.."====wport=="..wPort)
    dv.LuaConnector:GetInstance(1):Connect(sIp,wPort)
end

function Account:onConnectResponse(wConnectID,wState)
    print("===OnConnectResponse===")
    dispatchEventCustom(ds.Account.evtConnectResponse, {wConnectID = wConnectID,wState = wState })
end

function Account:onMainHallLogin( wSubCmdID , packet )
    print("===Account:onMainHallLogin==wSubCmdID="..wSubCmdID)
    local data = {}
    local structName
    if(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_SUC ) then        -- 用户登录成功
       structName = 'CMD_SC_AgentUserLoginInfo'
       local structData = Struct:getInstance():getBodyData(structName)
       local data = Struct:getInstance():readBody(packet, structData)
      
       self.m_UserLoginInfo = data 
       print("=ddddddddddd==Account:onMainHallLogin==data.szName======"..ds.Account:getUserLoginInfo().szName)
       return true
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FINISH ) then -- 用户登录完成  --不带结构
        print("===Cmd.SUB_SC_USER_LOGIN_HALL_FINISH===")
    elseif(wSubCmdID == Cmd.SUB_SC_GAME_HALL_LIST_SUCC ) then    -- 获取大厅游戏信息 成功
       structName = 'CMD_SC_HALLGAME_LIST'       
       local structData = Struct:getInstance():getBodyData(structName)
       local data = Struct:getInstance():readBody(packet, structData)
       table.insert(self.m_GameList, data)
       return true
    elseif(wSubCmdID == Cmd.SUB_SC_GAME_HALL_LIST_FINISH ) then    -- 获取大厅游戏信息完成
        print("===Cmd.SUB_SC_GAME_HALL_LIST_FINISH===")
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FAIL ) then   -- 用户登录失败
       structName = 'CMD_SC_AgentUserLoginFail'
       local structData = Struct:getInstance():getBodyData(structName)
       data = Struct:getInstance():readBody(packet, structData)
    else 
        print("==error=Account:onMainHallLogin==structName is nil =") 
    end
     
    dispatchEventCustom(ds.Account.evtMainHallLogin, {wSubCmdID=wSubCmdID,data = data } )
end


return Account
